package com.ch.home.service.serviceImpl.userServiceImpl;

import com.ch.home.bean.Message;
import com.ch.home.mapper.userMessageMapper.MessageMapper;
import com.ch.home.service.serviceInterface.user.MessageService;
import com.github.pagehelper.PageHelper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.HashMap;
import java.util.LinkedList;

/**
 * Created by CHEN on 2019/9/1.
 */
@Service
public class MessageServiceImpl implements MessageService {

    @Autowired
    MessageMapper messageMapper;

    @Override
    public HashMap<Object, Object> sendMessage(Message message) {

        HashMap<Object, Object> result = new HashMap<>( );
        //三者一个为空则不能插入
        if (message.getSenderId( ).equals(null) || message.getReceiverId( ).equals(null) || message.getMessageContent( ).equals(null)) {
            result.put("message", "fail");
        }else {
            int re = messageMapper.send(message);
            if (re == 1) {
                result.put("message", "success");
            } else {
                result.put("message", "fail");
            }
        }
        return result;
    }

    @Override
    public HashMap<Object, Object> receiveMessage(Message message) {


        HashMap<Object, Object> result = new HashMap<>( );
        LinkedList<Message> messages = messageMapper.recieve(message);
        if (messages.size( ) > 1) {
            result.put("message", "success");
        } else {
            result.put("message", "fail");
        }


        Message message1 = new Message();
        //2=已读
        //状态标为已读
        message1.setStatus(2);
        for (Message m:messages
             ) {
            if (m.getStatus()==1||m.getStatus()==0){
                message1.setMessageId(m.getMessageId());
                this.updateMessage(message1);
            }
        }
        result.put("value", messages);

        return result;


    }

    @Override
    public HashMap<Object, Object> getMessageHistory(Message message, int limit) {


        HashMap<Object, Object> result = new HashMap<>( );

        PageHelper pageHelper = new PageHelper( );
        //一次5条记录
        pageHelper.startPage(limit, 5);
        LinkedList<Message> messages = messageMapper.recieve(message);
        if (messages.size( ) > 1) {
            result.put("message", "success");
        } else {
            result.put("message", "fail");
        }

        Message message1 = new Message();
        //2=已读
        //状态标为已读
        message1.setStatus(2);
        for (Message m:messages
                ) {
            if (m.getStatus()==1||m.getStatus()==0){
                message1.setMessageId(m.getMessageId());
                this.updateMessage(message1);
            }
        }
        result.put("value", messages);

        return result;

    }

    /**
     * 状态的改变
     *
     * @param message
     * @return
     */
    @Override
    public Integer updateMessage(Message message) {

        HashMap<Object, Object> result = new HashMap<>( );
        int i = messageMapper.updateStatus(message);


        return i;
    }
}
